﻿#include "GAF_CommandFace.h"
#include "GAF_Application.h"
#include "GAF_Command.h"
#include <math.h>

#ifndef M_PI
#define M_PI			3.14159265358979323846
#endif

namespace GAF
{
	CommandFace** CommandFace::ms_ppFocus = NULL;
		
	CommandFace::CommandFace()
	{
		m_nGType = GRAPHICS_COMMANDFACE;
	}

	CommandFace::~CommandFace()
	{
	}

	void CommandFace::AddGraphics(Graphics* pGraphics)
	{
		GAF_CHECK(pGraphics != NULL);

		if(pGraphics->GetGType() == GRAPHICS_COMMANDFACE)
		{
			m_lnkCommandFace.Add((CommandFace*)pGraphics);
		}

		BASE(Face) AddGraphics(pGraphics);
	}

	void CommandFace::KeyDown(ULONG lCode)
	{
		GAF_KEY_DOMARK(lCode);
		(*ms_ppFocus)->OnKeyDown(lCode);
	}

	void CommandFace::KeyUp(ULONG lCode)
	{
		GAF_KEY_UNMARK(lCode);
		(*ms_ppFocus)->OnKeyUp(lCode);
	}

	void CommandFace::CursorDown(ULONG lCode, ULONG lAt)
	{
		GAF_CURSOR_DOMARK(lCode);
		(*ms_ppFocus)->OnCursorDown(lCode, GAF_PARAM_HIGH(lAt), GAF_PARAM_LOW(lAt));

#ifdef GAF_COMMAND_SCEEN
		// 屏幕命令操作
		if(lCode == GAF_CURSOR_LEFT)
		{
			DeviceState::isSwing = true;
			DeviceState::atSwingFrist.Set(GAF_PARAM_HIGH(lAt), GAF_PARAM_LOW(lAt));
			DeviceState::ttSwingFirst = GAF_pTHEPLATFORM->GetTimeTicks();
		}
#endif /*GAF_COMMAND_SCEEN*/
	}

	void CommandFace::CursorUp(ULONG lCode, ULONG lAt)
	{
		GAF_CURSOR_UNMARK(lCode);
		(*ms_ppFocus)->OnCursorUp(lCode, GAF_PARAM_HIGH(lAt), GAF_PARAM_LOW(lAt));

#ifdef GAF_COMMAND_SCEEN
		// 屏幕命令操作
		if(lCode == GAF_CURSOR_LEFT)
		{
			if(DeviceState::isSwing && DeviceState::vtrSwingRecord.size() >= 20)
			{
				GAF_DTimeTicks _ttNow = GAF_pTHEPLATFORM->GetTimeTicks();
				if(_ttNow - DeviceState::ttSwingLast <= 300)
				{
					int _nX = GAF_PARAM_HIGH(lAt);
					int _nY = GAF_PARAM_LOW(lAt);
					int _nXChange = _nX - DeviceState::atSwingFrist.nX;
					int _nYChange = _nY - DeviceState::atSwingFrist.nY;
					double _dWay = (double)WayAngle(DeviceState::atSwingFrist.nX, DeviceState::atSwingFrist.nY, _nX, _nY);
					int _nLong = (int)_hypot((float)abs(_nXChange), (float)abs(_nYChange));
					double _dPower = _nLong / (_ttNow - DeviceState::ttSwingFirst);
					OnSceenSwing(_dWay, _dPower);
				}
			}
			DeviceState::isSwing = false;
			DeviceState::vtrSwingRecord.clear();
		}
#endif /*GAF_COMMAND_SCEEN*/
	}

	void CommandFace::CursorMove(ULONG lAt)
	{
		int _nX = GAF_PARAM_HIGH(lAt);
		int _nY = GAF_PARAM_LOW(lAt);
		int _nXChange = _nX - DeviceState::atCursorLast.nX;
		int _nYChange = _nY - DeviceState::atCursorLast.nY;

		(*ms_ppFocus)->OnCursorMove(_nX, _nY, _nXChange, _nYChange);

#ifdef GAF_COMMAND_SCEEN
		// 屏幕命令操作
		if(GAF_CURSOR_ISMARK(GAF_CURSOR_LEFT))
		{
			double _dWay = (double)WayAngle(DeviceState::atCursorLast.nX, DeviceState::atCursorLast.nY, _nX, _nY);
			int _nLong = (int)_hypot((float)abs(_nXChange), (float)abs(_nYChange));
			
			// 划屏命令
			(*ms_ppFocus)->OnSceenSlide(_dWay, _nLong);

			// 甩屏判定与记录
			if(DeviceState::isSwing)
			{
				// 判断间隔时间
				GAF_DTimeTicks _ttNow = GAF_pTHEPLATFORM->GetTimeTicks();
				if(_ttNow - DeviceState::ttSwingLast > 300)
				{ // 如果间隔时间大于300毫秒则放弃
					DeviceState::isSwing = false;
					goto GAF_GOTO_CURSORMOVE_ENDSWING;
				}

				// 拥有前记录时
				if(DeviceState::vtrSwingRecord.size() > 0)
				{
					SwingRecord::const_iterator _cit = DeviceState::vtrSwingRecord.end() - 1;
					// 判断甩动方向是否为直线方向，距离是否为增长趋势
					if(abs(_dWay - _cit->dWay) > 5 || _nLong < _cit->nLong)
					{ // 如果方向偏差正负5度，或移动距离小于上次，则放弃
						DeviceState::isSwing = false;
						goto GAF_GOTO_CURSORMOVE_ENDSWING;
					}
				}

				// 添加记录
				SceenMove _sm = {_dWay, _nLong};
				DeviceState::vtrSwingRecord.push_back(_sm);

				// 记录时间
				DeviceState::ttSwingLast = _ttNow;
			}
GAF_GOTO_CURSORMOVE_ENDSWING:
			int _nTemp = 0;
		}
#endif /*GAF_COMMAND_SCEEN*/

		// 记录移动位置
		DeviceState::atCursorLast.Set(_nX, _nY);
	}

	int CommandFace::WayAngle(int nBX, int nBY, int nEX, int nEY)
	{ 
		int _dAngle = (int)(atan2((double)abs(nBX - nEX), (double)abs(nBY - nEY)) * 180 / M_PI);

		if(nEX >= nBX)
		{ // 在第一和第二象限
			if(nEY >= nBY)
			{ // 第二象限
				_dAngle = 180 - _dAngle;
			}
		}
		else
		{ //在第三和第四象限
			if (nEY > nBY)
			{ //第三象限
				_dAngle = 180 + _dAngle;
			}
			else
			{ //第四象限
				_dAngle = 360 - _dAngle;
			}
		}

		return _dAngle;
	}



}